//
//  CLoginView.m
//  HCHK_iphone
//
//  Created by yilong zhang on 11-12-31.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CLoginView.h"

@implementation CLoginView

@synthesize m_BackView;
@synthesize m_PanelView;
@synthesize m_NamePanel;
@synthesize m_RegisterBtn;
@synthesize m_LoginBtn;
@synthesize m_LogoutBtn;
@synthesize m_LoginPwd;
@synthesize m_LoginName;
@synthesize m_Email;
@synthesize m_FindPwdBtn;
@synthesize m_FindComitBtn;
@synthesize m_FindCancelBtn;

@synthesize m_Target;
@synthesize m_Action;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
        //self.backgroundColor = [UIColor clearColor];//[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
        UIImageView *bgimg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        bgimg.image = [UIImage imageNamed:@"appbg.jpg"];
        [self addSubview:bgimg];
        [bgimg release];
        
        self.m_Title = @"登陆好吃客";
        
        m_BackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        //m_BackView.backgroundColor = [UIColor blackColor];
        [self addSubview:m_BackView];
        [m_BackView release];
        
        bgimg = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, 199, 21)];
        bgimg.image = [UIImage imageNamed:@"welcom.png"];
        [self addSubview:bgimg];
        [bgimg release];
        
        m_NamePanel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        m_NamePanel.backgroundColor = self.backgroundColor;
        [m_BackView addSubview:m_NamePanel];
        [m_NamePanel release];
        
        /*UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, frame.size.width-40, 30)];
        title.text = @"找回密码";
        title.font = font;
        title.textAlignment = UITextAlignmentCenter;
        title.backgroundColor = [UIColor clearColor];
        [m_NamePanel addSubview:title];
        [title release];
        
        UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(20, 100, frame.size.width-40, 50)];
        bg.backgroundColor = [UIColor whiteColor];
        bg.layer.cornerRadius = 10;
        bg.layer.borderColor  = [[UIColor lightGrayColor] CGColor];
        bg.layer.borderWidth  = 1;
        [m_NamePanel addSubview:bg];
        [bg release];
        
        bgimg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 45, 244, 30)];
        bgimg.center = CGPointMake(frame.size.width/2, bgimg.center.y);
        bgimg.image = [UIImage imageNamed:@"loginemail.png"];
        [m_NamePanel addSubview:bgimg];
        [bgimg release];
        
        m_Email = [[CTextField alloc] initWithFrame:CGRectMake(98, 48, 180, 25)];
        //m_Email.center = CGPointMake(frame.size.width/2, m_Email.center.y);
        //m_Email.placeholder = @"注册邮箱";
        m_Email.font = [UIFont systemFontOfSize:15];
        m_Email.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        m_Email.borderStyle = UITextBorderStyleNone;
        m_Email.returnKeyType = UIReturnKeyDone;
        m_Email.keyboardType = UIKeyboardTypeEmailAddress;
        m_Email.delegate = self;
        [m_NamePanel addSubview:m_Email];
        [m_Email release];
                
        m_FindComitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_FindComitBtn.frame = CGRectMake(bgimg.frame.origin.x, bgimg.frame.origin.y+bgimg.frame.size.height+15, 85, 22);
        //[m_FindComitBtn setTitle:@"提交" forState:UIControlStateNormal];
        m_FindComitBtn.tag = 4;
        [m_FindComitBtn setBackgroundImage:[UIImage imageNamed:@"findpwd.png"] forState:UIControlStateNormal];
        [m_FindComitBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_NamePanel addSubview:m_FindComitBtn];
        
        m_FindCancelBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        m_FindCancelBtn.frame = CGRectMake(frame.size.width-20-100, m_Email.frame.origin.y+m_Email.frame.size.height+55, 100, 35);
        [m_FindCancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        m_FindCancelBtn.tag = 5;
        [m_FindCancelBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_NamePanel addSubview:m_FindCancelBtn];
        */
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        
        bgimg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, m_BackView.frame.size.width, m_BackView.frame.size.height)];
        bgimg.image = [UIImage imageNamed:@"appbg.jpg"];
        [m_BackView addSubview:bgimg];
        [bgimg release];
        
        m_PanelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        m_PanelView.backgroundColor = self.backgroundColor;
        [m_BackView addSubview:m_PanelView];
        [m_PanelView release];

        bgimg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 45, 244, 30)];
        bgimg.center = CGPointMake(frame.size.width/2, bgimg.center.y);
        bgimg.image = [UIImage imageNamed:@"loginemail.png"];
        [m_PanelView addSubview:bgimg];
        [bgimg release];
        
        m_LoginName = [[CTextField alloc] initWithFrame:CGRectMake(97, 48, 180, 25)];
        //m_LoginName.center = CGPointMake(frame.size.width/2, m_LoginName.center.y);
        //m_LoginName.placeholder = @"用户名/邮箱";
        m_LoginName.font = [UIFont systemFontOfSize:15];
        m_LoginName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        m_LoginName.borderStyle = UITextBorderStyleNone;
        m_LoginName.returnKeyType = UIReturnKeyDone;
        m_LoginName.keyboardType = UIKeyboardTypeDefault;
        m_LoginName.delegate = self;
        [m_LoginName addTarget:self action:@selector(OnTextFieldChanged:) forControlEvents:UIControlEventEditingChanged];
        [m_PanelView addSubview:m_LoginName];
        [m_LoginName release];
        
        bgimg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 95, 244, 30)];
        bgimg.center = CGPointMake(frame.size.width/2, bgimg.center.y);
        bgimg.image = [UIImage imageNamed:@"loginpwd.png"];
        [m_PanelView addSubview:bgimg];
        [bgimg release];

        m_LoginPwd = [[CTextField alloc] initWithFrame:CGRectMake(98, 98, 180, 25)];
        //m_LoginPwd.center = CGPointMake(frame.size.width/2, m_LoginPwd.center.y);
        //m_LoginPwd.placeholder = @"密码";
        m_LoginPwd.font = [UIFont systemFontOfSize:15];
        m_LoginPwd.secureTextEntry = YES;
        m_LoginPwd.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        m_LoginPwd.borderStyle = UITextBorderStyleNone;
        m_LoginPwd.returnKeyType = UIReturnKeyDone;
        m_LoginPwd.delegate = self;
        [m_LoginPwd addTarget:self action:@selector(OnTextFieldChanged:) forControlEvents:UIControlEventEditingChanged];
        [m_PanelView addSubview:m_LoginPwd];
        [m_LoginPwd release];
        
        /*m_LoginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        m_LoginBtn.frame = CGRectMake(20, m_LoginName.frame.origin.y+m_LoginName.frame.size.height+85, 100, 35);
        [m_LoginBtn setTitle:@"登陆" forState:UIControlStateNormal];
        m_LoginBtn.tag = 1;
        [m_LoginBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_PanelView addSubview:m_LoginBtn];*/
        
        /*CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = CGRectMake(1, 1, m_LoginBtn.frame.size.width, m_LoginBtn.frame.size.height);
        gradient.borderWidth = 1;
        gradient.borderColor = [[UIColor grayColor] CGColor];
        gradient.cornerRadius = 8;
        gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor lightGrayColor] CGColor], nil];
        [m_LoginBtn.layer addSublayer:gradient];*/
        
        m_RegisterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_RegisterBtn.frame = CGRectMake(231, 140, 37, 22);
        [m_RegisterBtn setBackgroundImage:[UIImage imageNamed:@"registerbtn.png"] forState:UIControlStateNormal];
        m_RegisterBtn.tag = 2;
        [m_RegisterBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_PanelView addSubview:m_RegisterBtn];
        
        /*m_LogoutBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        m_LogoutBtn.frame = CGRectMake(20, m_LoginName.frame.origin.y+m_LoginName.frame.size.height+85, bg.frame.size.width, 35);
        [m_LogoutBtn setTitle:@"登出" forState:UIControlStateNormal];
        m_LogoutBtn.tag = 3;
        m_LogoutBtn.hidden = YES;
        [m_LogoutBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_PanelView addSubview:m_LogoutBtn];*/
        
        m_FindPwdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_FindPwdBtn.frame = CGRectMake(97, 140, 87, 22);
        [m_FindPwdBtn setBackgroundImage:[UIImage imageNamed:@"forgetpwdbtn.png"] forState:UIControlStateNormal];
        m_FindPwdBtn.tag = 0;
        [m_FindPwdBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_PanelView addSubview:m_FindPwdBtn];
        
        /*UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, m_FindPwdBtn.frame.size.width, m_FindPwdBtn.frame.size.height)];
        lb.backgroundColor = [UIColor clearColor];
        lb.textColor = [UIColor colorWithRed:0.114 green:0.4 blue:0.419 alpha:1.0];
        lb.text = @"忘记密码了?";
        lb.font = [UIFont systemFontOfSize:15];
        [m_FindPwdBtn addSubview:lb];
        [lb release];*/
    }
    
    return self;
}

-(void)dealloc
{
    [m_LoginName release];
    
    [super dealloc];
}

-(void)OnButtonDown:(UIButton*)sender
{
    if (/*5 == sender.tag || */0 == sender.tag)
    {
        /*[UIView beginAnimations:nil context:nil];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:m_BackView cache:YES];
        [UIView setAnimationDuration:1];
        m_Email.text = nil;
        [m_BackView exchangeSubviewAtIndex:2 withSubviewAtIndex:0];
        [UIView commitAnimations];*/
        [m_Target performSelector:m_Action withObject:[NSNumber numberWithInt:sender.tag] withObject:nil];
        [self HiddenKeyboard];
    }
    else 
    {
        m_LoginPwd.text = [m_LoginPwd.text lowercaseString];
        [m_Target performSelector:m_Action withObject:[NSNumber numberWithInt:sender.tag] withObject:m_Email.text];
    
        [self HiddenKeyboard];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    /*if (self.frame.size.height/2 == m_PanelView.center.y)
    {
        [UIView beginAnimations:nil context:nil];
        m_PanelView.center = CGPointMake(m_PanelView.center.x, m_PanelView.center.y-30);
        [UIView commitAnimations];
    }*/
}
/*
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(cut:))
    {
        return NO;
    } 
    else if(action == @selector(copy:))
    {
        return YES;
    } 
    else if(action == @selector(paste:))
    {
        return NO;
    } 
    else if(action == @selector(select:))
    {
        return NO;
    } 
    else if(action == @selector(selectAll:))
    {
        return NO;
    }
 
    return NO;
}
*/
-(void)OnTextFieldChanged:(UITextField*)textfield
{
    if ([textfield.text length] > 0)
    {
        if (m_LoginName == textfield)
        {
            [textfield IsNameRightFormat:30];
        }
        else if (m_LoginPwd == textfield)
        {
            [textfield IsPwdRightFormat:18];
        }
        else if (m_Email == textfield)
        {
            return;
        }
        else {;}
        
        /*NSString *emailRegex = @"^\\w+$"; 
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
        
        NSRange range     = {[textfield.text length]-1, 1};
        NSString *value   = [textfield.text substringWithRange:range];
        NSString *set     = nil;
        
        中文
        if ([[AppConfig DefaultAppConfig] IsContainChinese:value])
        {
            range.length   = [textfield.text length]-1;
            range.location = 0;
            set = [NSString stringWithString:[textfield.text substringWithRange:range]];
            textfield.text = set;
        }
        else if ([value isEqualToString:@"."] || [value isEqualToString:@"_"])
        {
            return;
        }
        else if (![emailTest evaluateWithObject:value])
        {
            range.length   = [textfield.text length]-1;
            range.location = 0;
            set = [NSString stringWithString:[textfield.text substringWithRange:range]];
            textfield.text = set;
        }
        else {;}*/
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self HiddenKeyboard];
        
    return YES;
}

-(void)HiddenKeyboard
{
    [m_LoginName resignFirstResponder];
    [m_LoginPwd resignFirstResponder];
    [m_Email resignFirstResponder];
    
    /*if (self.frame.size.height/2 != m_PanelView.center.y)
    {
        [UIView beginAnimations:nil context:nil];
        m_PanelView.center = CGPointMake(m_PanelView.center.x, self.frame.size.height/2);
        [UIView commitAnimations];
    }*/
}

-(void)SetLoginViewInfo:(CUser*)user
{
    if (Logined == user.m_UType)
    {
        if (nil == m_LoginName.text)
        {
            m_LoginName.text = user.U_Name;
            m_LoginPwd.text  = user.U_Pas;
            
            m_LogoutBtn.hidden   = NO;
            m_LoginBtn.hidden    = YES;
            m_FindPwdBtn.hidden  = YES;
            m_RegisterBtn.enabled= YES;
            m_LoginName.enabled  = NO;
            m_LoginPwd.enabled   = NO;
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
